<template>
  <div class="container">
    <div class="app-container">
      <div class="left">
        <el-input v-model="queryParams.keyword" style="margin-bottom:10px" type="text" prefix-icon="el-icon-search" size="small" placeholder="输入员工姓名全员搜索" @input="changeValue" />
        <!-- 树形组件 -->
        <el-tree
          ref="tree"
          :expand-on-click-node="false"
          default-expand-all
          node-key="id"
          :data="depts"
          :props="defaultProps"
          highlight-current
          @current-change="onCurrentChange"
        />
      </div>
      <div class="right">
        <el-row class="opeate-tools" type="flex" justify="end">
          <el-button v-permisson="'add-emp'" size="mini" type="primary" @click="$router.push('/employee/detail')">添加员工</el-button>
          <el-button size="mini" @click="showExcelDialog=true">excel导入</el-button>
          <el-button size="mini" @click="exportEmployee">excel导出</el-button>
        </el-row>
        <!-- 表格组件 -->
        <el-table :data="tableData" stripe>
          <el-table-column align="left" type="selection" />
          <el-table-column align="center" label="头像">
            <template v-slot="{row}">
              <img v-if="row.staffPhoto" class="username" :src="row.staffPhoto" alt="">
              <img v-else class="username" :src="defaultImg" alt="">
            </template>
          </el-table-column>
          <el-table-column align="left" prop="username" label="姓名" />
          <el-table-column align="left" prop="mobile" label="手机号" sortable />
          <el-table-column align="left" prop="workNumber" label="工号" sortable />
          <el-table-column align="left" prop="formOfEmployment" label="聘用形式">
            <template v-slot="{row}">
              <span>{{ row.formOfEmployment===1?'正式':row.formOfEmployment===2?'非正式':'未说明' }}</span>
            </template>
          </el-table-column>
          <el-table-column align="left" prop="departmentName" label="部门" />
          <el-table-column align="left" prop="timeOfEntry" label="入职时间" sortable />
          <el-table-column align="left" label="操作">
            <template v-slot="{row}">
              <el-row type="flex">
                <el-button type="text" size="mini" @click="$router.push(`/employee/detail?id=${row.id}`)">查看</el-button>
                <el-button type="text" size="mini" @click="showRole(row.id)">角色</el-button>
                <el-popconfirm
                  title="这是一段内容确定删除吗？"
                  @onConfirm="delEmp(row.id)"
                >
                  <el-button slot="reference" style="margin-left: 10px;" type="text" size="mini">删除</el-button>
                </el-popconfirm>
              </el-row>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页 -->
        <el-row
          type="flex"
          justify="end
        "
          style="height: 60px;"
          align="middle"
        >
          <el-pagination
            layout="total,prev, pager, next"
            :total="total"
            :current-page="queryParams.page"
            :page-size="queryParams.pagesize"
            @current-change="currentChange"
          /></el-row>

      </div>
    </div>
    <import-excel :show-excel-dialog.sync="showExcelDialog" @success=" getEmpList" />
    <el-dialog title="分配角色" :visible.sync="showRoleDialog">
      <el-checkbox-group v-model="checkList">

        <el-checkbox v-for="item in enableList" :key="item.id" :label="item.id">{{ item.name }}</el-checkbox>
      </el-checkbox-group>
      <el-row slot="footer" type="flex" justify="center">
        <el-col :span="6">
          <el-button type="primary" size="mini" @click="submit">确定</el-button>
          <el-button size="mini" @click="showRoleDialog=false">取消</el-button></el-col>
      </el-row>
    </el-dialog>
  </div>
</template>

<script>
import { getDeptList } from '@/api/department'
import { transListToTreeData } from '@/utils/index'
import { getEmpList, exportEmployee, delEmp, getEnabledRole, getEmpInfo, assignRole } from '@/api/employee'
import defaultImg from '@/assets/common/avatar.webp'
import FileSaver from 'file-saver'
import importExcel from './components/import-excel.vue'
export default {
  name: 'Employee',
  components: {
    importExcel
  },
  data() {
    return {
      defaultImg,
      currentNodeId: null,
      showDialog: false,
      showExcelDialog: false,
      showRoleDialog: false,
      enableList: [],
      checkList: [],
      depts: [
      ],
      currentRoleId: null,
      defaultProps: {
        label: 'name',
        children: 'children'
      },
      // 存储查询参数
      queryParams: {
        page: 1,
        pagesize: 10,
        keyword: '',
        departmentId: null
      },
      tableData: [],
      total: null

    }
  },
  created() {
    this.getDepartment()
  },
  methods: {
    async getDepartment() {
      // 调用获取组织部门接口
      const res = await getDeptList()
      // 对数据进行递归处理
      this.depts = transListToTreeData(res, 0)
      this.queryParams.departmentId = this.depts[0].id
      // 设置选中节点
      // 树组件渲染是异步的 等到更新完毕
      this.$nextTick(() => {
        // 此时意味着树渲染完毕
        this.$refs.tree.setCurrentKey(this.queryParams.departmentId)
      })
      console.log(1111)
      this.getEmpList()
    },
    onCurrentChange(node) {
      this.queryParams.departmentId = node.id
      this.queryParams.page = 1
      this.getEmpList()
    },
    async getEmpList() {
      const { total, rows
      } = await getEmpList(this.queryParams)
      this.tableData = rows
      this.total = total
    },
    currentChange(value) {
      this.queryParams.page = value
      this.getEmpList()
    },
    changeValue() {
      // 进行防抖处理
      clearTimeout(this.timer)
      this.timer = setTimeout(() => {
        this.queryParams.page = 1
        this.getEmpList()
      }, 400)
    },
    async exportEmployee() {
      const res = await exportEmployee()

      // FileSaver.saveAs(blob对象,文件名称)
      FileSaver.saveAs(res, '员工信息表.xlsx') // 下载文件
    },
    async delEmp(id) {
      await delEmp(id)
      this.$message.success('删除成功')
      this.getEmpList()
    },
    async showRole(id) {
      this.currentRoleId = id
      this.enableList = await getEnabledRole()
      const { roleIds } = await getEmpInfo(id)
      this.checkList = roleIds
      this.showRoleDialog = true
    },
    async submit() {
      await assignRole(this.currentRoleId, this.checkList)
      this.$message.success('分配员工角色成功')
      this.showRoleDialog = false
    }

  }
}
</script>

<style lang="scss" scoped>
.app-container {
  background: #fff;
  display: flex;
  .left {
    width: 280px;
    padding: 20px;
    border-right: 1px solid #eaeef4;
  }
  .right {
    flex: 1;
    padding: 20px;
    .opeate-tools {
      margin:10px ;
    }
    .username {
      height: 30px;
      width: 30px;
      line-height: 30px;
      text-align: center;
      border-radius: 50%;
      color: #fff;
      background: #04C9BE;
      font-size: 12px;
      display:inline-block;
    }
  }
}

</style>
